package service

import (
	"github.com/jinzhu/gorm"
	"tylnmall/models"
	"common/logger"
	"errors"
)

type SpeciesList struct {
	Id   int                        `json:"id" form:"id"`
	Name string                     `json:"name" form:"name"`
	Spec []*models.CommoditySpecies `json:"spec"`
}



func SpeciesChildList(db *gorm.DB, cs []*SpeciesList) (err error, cd []*SpeciesList) {
	for _, c := range cs {
		var childs []*models.CommoditySpecies
		if err = db.Where("father_id = ?", c.Id).Find(&childs).Error; err != nil {
			logger.Error(err)
			err = errors.New("species not found")
			logger.Error(err)
			return err, cs
		}
		c.Spec = childs
	}
	return nil, cs
}

//判断是否存在该商品
func IsCommodity(db *gorm.DB, id int) (bool,models.Commodity){
	var c models.Commodity
	if err:= db.Where("id = ?",id).Find(&c).Error;err!=nil{
		return false,c
	}
	return true,c
}
